# Table_3.R
# Aim: to reproduce Figure 6 of Atsusaka and Holbrook (2026)

library(tidyverse)
library(kableExtra)

# read data --------------------------------------------------------------------
raw <- read_csv("archive-election.csv") %>%
  select(level, state, office, juris, n_cands, n_rounds, year)

# --- normalize level values ---------------------------------------------------
# make levels lowercase and underscore variants consistent (e.g., "School board" -> "school_board")
raw <- raw %>%
  mutate(
    level = tolower(level),
    level = str_replace_all(level, "\\s+", "_"),
    level = case_when(
      level %in% c("school", "schoolboard", "school_board") ~ "school_board",
      TRUE ~ level
    )
  )

# desired level order (now includes county)
level_order <- c("federal", "state", "county", "municipal", "school_board")

# summary table ----------------------------------------------------------------
dt_sum <- raw %>%
  mutate(level = forcats::fct_relevel(level, !!!level_order)) %>%
  group_by(level, state, juris, office) %>%
  summarise(
    n_cands  = sum(n_cands,  na.rm = TRUE),   # total candidates
    n_rounds = sum(n_rounds, na.rm = TRUE),   # total rounds
    n_races  = n(),                            # number of data points
    n_years  = max(year, na.rm = TRUE) - min(year, na.rm = TRUE) + 1, # span of years
    .groups = "drop"
  ) %>%
  mutate(across(c(n_cands, n_rounds, n_races, n_years), as.integer))

# order rows by the updated level order ----------------------------------------
ordered <- dt_sum %>%
  arrange(level, state, juris, office) %>%
  mutate(row_id = row_number())

# compute group start/end rows per level (auto) --------------------------------
group_info <- ordered %>%
  count(level, name = "n") %>%
  mutate(
    start = lag(cumsum(n), default = 0) + 1,
    end   = cumsum(n),
    label = case_when(
      level == "school_board" ~ "School board",
      TRUE ~ str_to_title(as.character(level))
    )
  ) %>%
  # keep only levels that actually appear in the data
  filter(n > 0)

# build the LaTeX table with grouped headers -----------------------------------
kbl_obj <- ordered %>%
  select(-level, -row_id) %>%
  kableExtra::kbl(booktabs = TRUE, longtable = TRUE, format = "latex")

# add group_rows for each level present
for (i in seq_len(nrow(group_info))) {
  kbl_obj <- kableExtra::group_rows(
    kbl_obj,
    group_label = group_info$label[i],
    start_row   = group_info$start[i],
    end_row     = group_info$end[i]
  )
}

kbl_obj
print(kbl_obj)
